<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/04/0004
 * Time: 16:38
 */
namespace app\api\model;
require_once './vendor/wxpay/lib/WxPay.Config.php';
require_once './vendor/wxpay/lib/WxPay.App.Config.php';
require_once './vendor/wxpay/lib/WxPay.Data.php';
require_once './vendor/wxpay/lib/WxPay.Api.php';
/*vendor('wxpay.lib.WxPay.Config');
vendor('wxpay.lib.WxPay.Data');
vendor('wxpay.lib.WxPay.Api');*/
class Wxpay extends BaseModel
{
    protected $config;
    public function __construct($data = [])
    {
        parent::__construct($data);
        if($data == 'MWEB'){
            $this->config = new \WxPayConfig();
        }else{
            $this->config = new \WxPayAppConfig();
        }

    }

    public function wchatPay($out_trade_no,$orderInfo,$trade_type)
    {
        if (! is_numeric($out_trade_no)) {
            AjaxReturn(-1,"没有获取到支付信息");
        }
        $red_url = $orderInfo['red_url'];
        $body = $orderInfo['body'];
        $inputObj = new \WxPayUnifiedOrder();
        $inputObj->SetBody($body);
        $inputObj->SetOut_trade_no($out_trade_no);
        $inputObj->SetTotal_fee($orderInfo['number']*100);
        $inputObj->SetTrade_type($trade_type);
        $inputObj->SetNotify_url($red_url);
        $wxpayApiModel = new \WxPayApi();
        $res = $wxpayApiModel->unifiedOrder($this->config,$inputObj,6);
        if($trade_type=='APP'){
            //app支付二次签名
            $data =array(
                'prepayid' =>$res['prepay_id'],
                'appid' => $this->config->GetAppId(),
                'partnerid' => $this->config->GetMerchantId(),
                'package' => 'Sign=WXPay',
                'noncestr' => $res['nonce_str'],
                'timestamp' =>time(),
            );
            $sign = $this->MakeSign($data);
            $data['sign'] = $sign;
            return $data;
        }else{
            return $res['mweb_url'];
        }


    }
    /**
     * 生成签名
     * @param WxPayConfigInterface $config  配置对象
     * @param bool $needSignType  是否需要补signtype
     * @return 签名，本函数不覆盖sign成员变量，如要设置签名需要调用SetSign方法赋值
     */
    public function MakeSign($data)
    {
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string = $this->ToUrlParams($data);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=".$this->config->GetKey();
        //签名步骤三：MD5加密或者HMAC-SHA256
        if($this->config->GetSignType() == "MD5"){
            $string = md5($string);
        } else if($this->config->GetSignType() == "HMAC-SHA256") {
            $string = hash_hmac("sha256",$string ,$this->config->GetKey());
        } else {
            throw new \WxPayException("签名类型不支持！");
        }

        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams($data)
    {
        $buff = "";
        foreach ($data as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
}